<?php
/**
 *      [迷你云] (C)2009-2012 南京恒为网络科技.
 *   软件仅供研究与学习使用，如需商用，请访问www.miniyun.cn获得授权
 * 
 */
?>
<?php

class FileBase extends MiniyunViewForm
{
	public $id;

	public $userID;
	public $parentID;
	public $retreatID;	public $filePath;

	public $icon;
	public $fileUuid;
	public $fileName;
	public $author;
	public $directory;
	public $isFolder;
	public $fileSize;
	public $isDeleted;
	public $versionID;
	public $updatedAt;
	public $createdAt;
	
	private function _getParentPath(){
	  	$userRoot   = "/".$this->userID;
	  	$name       = $this->fileName;
	  	$retVal   = $this->filePath;
	  	$retVal = substr($retVal,strlen($userRoot));
	  	$retVal = substr($retVal,0,strlen($retVal)-strlen($name));
	  	return $retVal;
	}
	
	public function load($fileId)
	{
		$fileItem = UserFile::model()->findByAttributes(array('id'=>$fileId));
		if (empty($fileItem))
		{
			return;
		}
		$this->id        = $fileId; 
		$this->directory = $fileItem['file_path'];
		$userModel = User::model()->findByPk($fileItem['user_id']);
		if (!empty($userModel))
		{
			$this->author = $userModel['user_name'];
			$this->userID = $fileItem['user_id'];
		}
		$this->parentID   = $fileItem['parent_file_id'];
		if($fileItem['parent_file_id']!==0)
		{ 
			$parentFile   = UserFile::model()->findByPk($fileItem['parent_file_id']);
			if (!empty($parentFile))
			{
				$this->retreatID = $parentFile['parent_file_id'];			}
		}
        
		$this->fileName  = $fileItem['file_name'];
		$this->filePath  = $fileItem['file_path'];
		$this->isFolder  = $fileItem['file_type'];

		if ($this->isFolder)
		{
			$this->fileSize = '文件夹';
		} else
		{
			if ($fileItem['file_size'] === 0)
			{
				$this->fileSize = '空文件';
			} else
			{
				$this->fileSize  = CUtils::getFormatSize($fileItem['file_size']);
			}
		}
		$this->versionID = $fileItem['version_id'];
		$this->isDeleted = $fileItem['is_deleted'];
		$this->updatedAt = CUtils::getFormatTime($fileItem['file_update_time']);
		$this->createdAt = CUtils::getFormatTime($fileItem['file_create_time']);
		if ($this->isFolder)
		{
			$this->icon=Yii::app()->params["iconFolder"];
		} else
		{
			$this->icon=Yii::app()->params["iconFile"];
		}
		if (!isset($this->author))
		{
			{
				$this->author = "无名";			}
		}
		if (!isset($this->retreatID))
		{
			$this->retreatID = 0;
		}
	}
	
	public function attributeLabels()
	{
		return array(
                'fileName'  => '文件名',
                'directory' => '上级目录',
                'fileSize'  => '文件大小',
            	'author'    => '创建者', 
                'updatedAt' => '文件最近修改时间',

		);
	}
	
	public function getItemOperates(){

										$action = 'index';
		$pageArr = array();
		$page = Yii::app()->request->getParam('page');
		if (isset($page))
		{
			$pageArr['page'] = $page;
		}
		$office = Yii::app()->request->getParam('office');
		if (isset($office))
		{
			$action = 'office&offic=1';
		}
		$image = Yii::app()->request->getParam('image');
		if (isset($image))
		{
			$action = 'image&image=1';
		}
		$trash = Yii::app()->request->getParam('trash');
		if (isset($trash))
		{
			$action = 'trash&trash=1';
		}
		$directory = Yii::app()->request->getParam('directory');
		if (isset($directory))
		{
			$pageArr = array_merge($pageArr, array('directory'=>$directory));
		}
		$isRaname = Yii::app()->request->getParam('rename');
		if (isset($isRaname) && $this->id === $isRaname)
		{
			return;
		}


		$retVal = "<a href='".Yii::app()->createUrl("adminFilePanel/show", array('id'=>$this->id))."'>详情</a>";
		$retVal.= " | <a href='".Yii::app()->createUrl("adminFilePanel/".$action, array_merge(array('rename'=>$this->id),$pageArr))."'>更名</a>";

		if ($this->isDeleted)
		{
			$retVal.= " | <a href='".Yii::app()->createUrl("adminFilePanel/revert", array_merge(array('id'=>$this->id),$pageArr))."'>还原</a>";
		} else
		{
			$retVal.= " | <a href='".Yii::app()->createUrl("adminFilePanel/remove",array_merge(array('id'=>$this->id),$pageArr))."'>删除</a>";
		}

		if ($this->isFolder)
		{
			$retVal.= " | <a href='".Yii::app()->createUrl("adminFilePanel/".$action, array('directory'=>$this->id))."'>进入</a>";
		} else
		{
			$retVal.= " | <a href='".Yii::app()->createUrl("adminFilePanel/download", array_merge(array('action'=>$action, 'id'=>$this->id, 'version'=>$this->versionID, 'name'=>$this->fileName), $pageArr))."'>下载</a>";
		}


		$inFolder = Yii::app()->request->getParam('directory');
		if (isset($inFolder) && $inFolder != 0)
		{
			$retVal = "<a href='".Yii::app()->createUrl("adminFilePanel/".$action, array('directory'=>$this->retreatID))."'>返回</a> | ".$retVal;
		}
		return $retVal;
	}
	
	public function fileNameDetail()
	{
		$name = $this->fileName;
		$action = Yii::app()->request->getParam('rename');
		if (isset($action) && $this->id === $action)
		{
			return "<img src='".Yii::app()->request->baseUrl.$this->icon."' style='width:48px'>".
			CHtml::hiddenField("FileListForm[renameID]", $this->id).
			CHtml::textField("FileListForm[renameKey]", $name).
			CHtml::submitButton("保存",array("class"=>"button-secondary action","name"=>"renameAction")).
			CHtml::submitButton("取消",array("class"=>"button-secondary action","name"=>"cancelAction"));
		} else
		{
			if(strlen($name)>23){
				$name = CUtils::getShowSubStr($name,22);
			}
		}
		$status = $this->isDeleted ? "<img src='".Yii::app()->request->baseUrl."/statics/images/delete.png' style='width:48px'>":"<img src='".Yii::app()->request->baseUrl.$this->icon."' style='vertical-align: middle; display: inline;'>";
		return $status.CHtml::link($name,Yii::app()->createUrl("adminFilePanel/show", array('id'=>$this->id)));
	}
	
	public function fileNameShow()
	{
		$name = $this->fileName;
		$action = Yii::app()->request->getParam('rename');
		if (isset($action) && $this->id === $action)
		{
			return "<div class='tablenav top'><div class='alignleft actions'>".
			CHtml::hiddenField("FileDetailForm[renameID]", $this->id).
			CHtml::textField("FileDetailForm[renameKey]", $name).
			CHtml::submitButton("保存",array("class"=>"button-secondary action","name"=>"renameAction")).
			CHtml::submitButton("取消",array("class"=>"button-secondary action","name"=>"cancelAction")).
            "</div></div>";
		}
		$status = $this->isDeleted ? "<img src='".Yii::app()->request->baseUrl."/statics/images/delete.png' style='width:16px'>":'';
		return $status.$this->fileName.
		CHtml::link(' 修改',Yii::app()->createUrl("adminFilePanel/show", array('id'=>$this->id, 'rename'=>$this->id)));
	}
	
	public function authorDetail()
	{
		return CHtml::link($this->author,Yii::app()->createUrl("adminFilePanel/index", array('author'=>$this->userID)));
	}
	
	public function authorShow()
	{
		return CHtml::link($this->author,Yii::app()->createUrl("adminUserPanel/show", array('id'=>$this->userID)));
	}
	
	public function directoryDetail()
	{
		$path = $this->_getParentPath($this->directory);
		if(strlen($path)>23){
			$path = CUtils::getShowSubStr($path,22);
		}
		return CHtml::link($path, Yii::app()->createUrl("adminFilePanel/index", array('directory'=>$this->parentID)));
	}

}
